<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/6/16
 * Time: 10:38
 */

namespace common\models\Service;


use common\models\CommonFunction;
use common\models\DAO\User;
use common\models\DAO\UserCode;
use common\msg\UserMsg;

class SendCode {
    public static function send($tel, $type = 1){
        $phone = CommonFunction::encrypt($tel);
        $code = CommonFunction::getRandChar(4, false);
        $error['status'] = 0;
        $userModel = User::findOne(array('phone'=>$phone));
        if($type == 1 && $userModel){
            $error['msg'] = UserMsg::$reg_already;
            return $error;
        }

        if($type == 2){
            if(!$userModel){
                $error['msg'] = UserMsg::$no_reg;
                return $error;
            }
            $userInfo = $userModel->attributes;
            if($userInfo['status'] == -1){
                $error['msg'] = UserMsg::$time_out;
                return $error;
            }
        }

        $today = time();
        $now = $today - 60;
        //array('condition' => 'tel=:state', 'params' => array(':state'=>$tel))
        $userCode = UserCode::find()->where(array('tel'=>$phone, 'type'=>$type))->andFilterWhere(['>', 'createTime', $now])->all();
        if($userCode){
            $error['status'] = 1;
            $error['msg'] = UserMsg::$send_already;
            return $error;
        }
        $send_content = UserMsg::$send_code;
        if($type == 2)
            $send_content = UserMsg::$forget_content;
        $content = str_replace('%c', $code, $send_content);
        $send_result = CommonFunction::sendCode($tel, $content);
        if($send_result != 0){
            $error['msg'] = UserMsg::$time_out;
            return $error;
        }

        $codeModel = new UserCode();
        $codeModel->code = $code;
        $codeModel->tel = $phone;
        $codeModel->createTime = $today;
        $codeModel->type = $type;
        $codeModel->ip = CommonFunction::get_client_ip();
        $codeModel->save();
        return array('status'=>1);
    }

    public static function hongbao($tel, $type = 3){
        $phone = CommonFunction::encrypt($tel);
        $code = CommonFunction::getRandChar(4, false);
        $error['status'] = 0;

        $today = time();
        $now = $today - 60;
        //array('condition' => 'tel=:state', 'params' => array(':state'=>$tel))
        $userCode = UserCode::find()->where(array('tel'=>$phone, 'type'=>$type))->andFilterWhere(['>', 'createTime', $now])->all();
        if($userCode){
            $error['status'] = 1;
            $error['msg'] = UserMsg::$send_already;
            return $error;
        }
        $send_content = UserMsg::$hongbao_content;
        $content = str_replace('%c', $code, $send_content);
        $send_result = CommonFunction::sendCode($tel, $content);
        if($send_result != 0){
            $error['msg'] = UserMsg::$time_out;
            return $error;
        }

        $codeModel = new UserCode();
        $codeModel->code = $code;
        $codeModel->tel = $phone;
        $codeModel->createTime = $today;
        $codeModel->type = $type;
        $codeModel->ip = CommonFunction::get_client_ip();
        $codeModel->save();
        return array('status'=>1);
    }
}